package com.j.lemon.learn.basic.dongtaidaili;


import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @Author lijunjun
 * @Date 2019-01-21 18:27
 * @Description
 */
public class JDKProxy implements InvocationHandler {
    private Object object;

    Object getInstance(Object o){
        this.object=o;
        return Proxy.newProxyInstance(o.getClass().getClassLoader(),o.getClass().getInterfaces(),this);
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("MyInvocationHandler invoke begin");
        System.out.println("proxy: "+ proxy.getClass().getName());
        System.out.println("method: "+ method.getName());
        if(args!=null){
            for(Object o : args){
                System.out.println("arg: "+ o);
            }
        }

        //通过反射调用 被代理类的方法
        method.invoke(object, args);
        System.out.println("MyInvocationHandler invoke end");
        return null;
    }
}
